<?php
/*
本系统由苏酷伊基于SiteHub项目二次开发
作者博客： https://www.sukuy.com/
后续更新地址： https://www.sukuy.com/navigation
作者QQ：3288637559
QQ交流群：884250547
任何第三方非官网在发布本系统时，不得删除该注释、版权信息、作者信息及官网等内容
*/
define('IN_ADMIN', true);
include("../includes/common.php");
if ($admin_islogin == 1) {
} else exit("<script language='javascript'>window.location.href='./login.php';</script>");

$act = _get('act');
switch ($act) {
    case 'add':
        $name = _post('name');
        $url = _post('url');
        $img = _post('img');
        if (!$name) {
            
            add_action_log($conf['admin_user'], '添加友情链接失败：名称不能为空', 0);
            exit("<script>alert('名称不能为空！');location='./link.php';</script>");
        }
        if (!$url) {
            
            add_action_log($conf['admin_user'], '添加友情链接失败：链接不能为空', 0);
            exit("<script>alert('链接不能为空！');location='./link.php';</script>");
        }
        
        $_data = array(
            'name' => $name,
            'url' => $url,
            'img' => $img
        );
        $result = $DB->insert('link', $_data);
        if ($result) {
            
            add_action_log($conf['admin_user'], '添加友情链接：' . $name, 1);
            exit("<script>alert('添加成功！');location='./link.php';</script>");
        } else {
            
            add_action_log($conf['admin_user'], '添加友情链接失败：' . $name, 0);
            exit("<script>alert('添加失败！');history.go(-1)</script>");
        }
        break;
    case 'edit':
        $id = _post('id');
        $row = $DB->find('link', '*', array('id' => $id));
        if (!$row) {
            
            add_action_log($conf['admin_user'], '编辑友情链接失败(ID: ' . $id . ')：友情链接不存在', 0);
            exit("<script>alert('链接不存在！');location='./link.php';</script>");
        }
        $name = _post('name');
        $url = _post('url');
        $img = _post('img');
        if (!$name) {
            
            add_action_log($conf['admin_user'], '编辑友情链接失败(ID: ' . $id . ')：名称不能为空', 0);
            exit("<script>alert('名称不能为空！');location='./link.php';</script>");
        }
        if (!$url) {
            
            add_action_log($conf['admin_user'], '编辑友情链接失败(ID: ' . $id . ')：链接不能为空', 0);
            exit("<script>alert('链接不能为空！');location='./link.php';</script>");
        }
        
        $_data = array(
            'name' => $name,
            'url' => $url,
            'img' => $img
        );
        $result = $DB->update('link', $_data, array('id' => $id));
        if ($result) {
            
            add_action_log($conf['admin_user'], '编辑友情链接(ID: ' . $id . ')：' . $name, 1);
            exit("<script>alert('修改成功！');location='./link.php';</script>");
        } else {
            
            add_action_log($conf['admin_user'], '编辑友情链接失败(ID: ' . $id . ')：' . $name, 0);
            exit("<script>alert('修改失败！');history.go(-1)</script>");
        }
        break;
    case 'del':
        @header('Content-Type: application/json; charset=UTF-8');
        $id = _post('id');
        if (!$id) {
            exit('{"code":-1,"msg":"ID不能为空！"}');
        }
        
        $link_info = $DB->find('link', 'name', array('id' => $id));
        $link_name = $link_info ? $link_info['name'] : '未知友情链接';
        $result = $DB->delete('link', array('id' => $id));
        if ($result) {
            
            add_action_log($conf['admin_user'], '删除友情链接(ID: ' . $id . ')：' . $link_name, 1);
            exit('{"code":0,"msg":"删除成功！"}');
        }
        
        add_action_log($conf['admin_user'], '删除友情链接失败(ID: ' . $id . ')：' . $link_name, 0);
        exit('{"code":-1,"msg":"删除失败！"}');
        break;
    default:
        @header('Content-Type: application/json; charset=UTF-8');
        exit('{"code":-4,"msg":"No Act"}');
        break;
}